﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ProgNetComponents.Interfaces;

namespace ProgNetComponents.Collections
{
    /// <summary>
    /// Base class for INamedObject collection
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseNamedCollection<T> : Collection<T> where T : Interfaces.INamedObject
    {
        /// <summary>
        /// Gets INamedObject with specified Name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public T this[string name]
        {
            get
            {
                foreach (T o in Items)
                    if (o.Name == name)
                        return o;
                return default(T);
            }
        }
        /// <summary>
        /// Specifies if INamedObject with specified name was added to collection
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Contains(string name)
        {
            return this[name] != null;
        }
        /// <summary>
        /// Removes INamedObject with specified name from collection
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Remove(string name)
        {
            T o = this[name];
            if (o != null)
                return Items.Remove(o);
            return false;
        }
        /// <summary>
        /// Adds all INamedObjects to collection
        /// </summary>
        /// <param name="objects"></param>
        public void AddRange(T[] objects)
        {
            if (objects != null)
            {
                foreach (T o in objects)
                {
                    Add(o);
                }
            }
        }

        /// <summary>
        /// Gets names of all INamedObjects in the collection
        /// </summary>
        public string[] Names
        {
            get
            {
                List<string> names = new List<string>();
                foreach (INamedObject o in this)
                    names.Add(o.Name);
                return names.ToArray();
            }
        }
        /// <summary>
        /// Inserts INamedObject at specified location
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        protected override void InsertItem(int index, T item)
        {
            if (item != null && !Contains(item.Name))
                base.InsertItem(index, item);
        }
        /// <summary>
        /// Sets INamedObject to specified at specified location
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        protected override void SetItem(int index, T item)
        {
            if (item != null && !Contains(item.Name))
                base.SetItem(index, item);
        }
        /// <summary>
        /// Returns index of INamedObject with specified name, or -1 if there is no INamedObject with specified name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int IndexOf(string name)
        {
            for (int i=0; i < this.Count; i++)
                if (Items[i].Name == name)
                    return i;
            return -1;
        }
    }
}
